<?php

namespace common\search;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\IcoProject;

/**
 * IcoProjectSearch represents the model behind the search form about `common\models\IcoProject`.
 */
class IcoProjectSearch extends IcoProject
{
    public $status = 0;
    public $sort = 0;

    const STATUS_ING = 1;

    const STATUS_COMPLETE = 2;

    const SORT_COMPLETE = 0;

    const SORT_FOLLOW = 1;

    const SORT_LEFT = 2;

    const SORT_COMPLETE_BILI = 3;
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'end_time', 'start_time', 'created_at','status','sort', 'updated_at'], 'integer'],
            [['name', 'base_desc', 'team_desc', 'white_book', 'code', 'publish_time', 'cankao_price', 'platform', 'allcount', 'level', 'unit', 'cover', 'fee'], 'safe'],
            [['count'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = IcoProject::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        switch($this->sort){
            case self::SORT_COMPLETE_BILI:
                //完成比例
                $query->orderBy([
                    'end_time' => SORT_ASC,
                    'left_count' => SORT_DESC,
                ]);
                break;
            case self::SORT_LEFT:
                //剩余量
                $query->orderBy([
                    'end_time' => SORT_ASC,
                    'left_count' => SORT_DESC,
                ]);
                break;
            case self::SORT_FOLLOW:
                //关注度
                $query->orderBy([
                    'end_time' => SORT_ASC,
                    'left_count' => SORT_DESC,
                ]);
                break;
            default:
            case self::SORT_COMPLETE:
                //已完成
                $query->orderBy([
                    'id' => SORT_DESC,
                ]);
                break;
        }

        $this->load($params,'');

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            $query->where('0=1');
            return $dataProvider;
        }

        switch($this->status){
            case self::STATUS_ING:
                $query->andFilterWhere([
                    '<',
                    'end_time',
                    time()
                ]);
                break;
            case self::STATUS_COMPLETE:
                $query->andFilterWhere([
                    '>=',
                    'end_time',
                    time()
                ]);
                break;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'count' => $this->count,
            'end_time' => $this->end_time,
            'start_time' => $this->start_time,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'base_desc', $this->base_desc])
            ->andFilterWhere(['like', 'team_desc', $this->team_desc])
            ->andFilterWhere(['like', 'white_book', $this->white_book])
            ->andFilterWhere(['like', 'code', $this->code])
            ->andFilterWhere(['like', 'publish_time', $this->publish_time])
            ->andFilterWhere(['like', 'cankao_price', $this->cankao_price])
            ->andFilterWhere(['like', 'platform', $this->platform])
            ->andFilterWhere(['like', 'allcount', $this->allcount])
            ->andFilterWhere(['like', 'level', $this->level])
            ->andFilterWhere(['like', 'unit', $this->unit])
            ->andFilterWhere(['like', 'cover', $this->cover])
            ->andFilterWhere(['like', 'fee', $this->fee]);

        return $dataProvider;
    }
}
